package com.ghpt.controller;
import com.ghpt.dto.ShiChangUpdateDTO;
import com.ghpt.pojo.ShiChang;
import com.ghpt.service.ShiChangService;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shichang")
public class ShiChangController {
    private final ShiChangService shiChangService;

    public ShiChangController(ShiChangService shiChangService) {
        this.shiChangService = shiChangService;
    }
    @GetMapping("/{id}")
    public ShiChang getShiChangById(@PathVariable Long id) {
        System.out.println("结果是"+shiChangService.findShiChangById(id));
        return shiChangService.findShiChangById(id);
    }
    @PutMapping
    public int updateShiChang(@RequestBody ShiChang shiChang) {
        return shiChangService.updateById(shiChang);
    }
    @GetMapping("/all")
    public List<ShiChang> getAllShiChang() {
        List<ShiChang> list = new ArrayList<>();
        for (long i = 1; i <= 96; i++) {
            list.add(shiChangService.findShiChangById(i));
        }
        return list;
    }
    @PutMapping("single/{id}")
    public int updateValueById(@PathVariable Long id, @RequestBody ShiChangUpdateDTO dto) {
        return shiChangService.updateValue(id, dto.getValue());
    }


    @PostMapping("/upload/{startId}")
    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file,
                                    @PathVariable("startId") Long startId) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("message", "文件为空"));
        }

        List<ShiChang> dataList = new ArrayList<>();

        try (Reader reader = new InputStreamReader(file.getInputStream());
             CSVParser csvParser = CSVFormat.DEFAULT.parse(reader)) {

            int count = 0;
            for (CSVRecord record : csvParser) {
                ShiChang entity = new ShiChang();
                entity.setId(startId + count); // 自动设置 ID 范围
                entity.setValue(Double.parseDouble(record.get(0).trim()));
                dataList.add(entity);
                count++;
            }

            if (count != 24) {
                return ResponseEntity.badRequest()
                        .body(Map.of("message", "数据条数不为24条！"));
            }

            shiChangService.replaceRows(startId, startId + 23, dataList);
            return ResponseEntity.ok(Map.of("message", "数据导入成功！"));

        } catch (IOException | NumberFormatException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("message", "文件解析失败或格式错误"));
        }
    }

}
